/*
 *   Time Tracking Web Application
 *   Copyright (C) 2008, Nick Griffiths
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   Contact nicobrevin at gmail dot com
 *   http://code.google.com/p/openharvest/
 */

package org.brevs.time.action.beans;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Dateinfo {

	private final Date today;
	
	private final Calendar calendar;

	private final SimpleDateFormat format;
	
	public Dateinfo(Date today) {
		this.today = today;
		calendar = Calendar.getInstance();
		format = new SimpleDateFormat();
		format.applyPattern("yyyyMMdd");
	}
	
	public Date getDay() {
		return today;
	}
	
	public String getDayTimestamp() {
		return getTimestamp(today);
	}
	
	public String getMinusWeek() {
		return getTimestamp(-7);
	}
	
	public String getMinusDay() {
		return getTimestamp(-1);
	}
	
	public String getPlusWeek() {
		return getTimestamp(7);
	}
	
	public String getPlusDay() {
		return getTimestamp(1);
	}
	
	public boolean isToday() {
		String today = format.format(new Date());
		String timestamp = format.format(this.today);
		return today.equals(timestamp);
	}
	
	private String getTimestamp(int dayshift) {
		return getTimestamp(this.today, dayshift);
	}
	
	private String getTimestamp(Date day) {
		return getTimestamp(day, 0);
	}
	
	private String getTimestamp(Date day, int dayshift) {
		calendar.setTime(day);
		calendar.add(Calendar.DATE, dayshift);
		return format.format(calendar.getTime());
	}

	public static Date parseTimestamp(String timestamp) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat();
		format.applyPattern("yyyyMMdd");
		return format.parse(timestamp);
	}
	
}
